package com.yifeng.repo.controller.excel.web;

import com.yifeng.repo.controller.excel.loader.ExcelLoaderManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.ServletContextInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Created by daibing on 2022/12/29.
 */
@Slf4j
public class ExcelLoaderServletRegistrar implements ServletContextInitializer {
    private final String dataTaskUriPrefix;
    private final ExcelLoaderManager excelLoaderManager;

    public ExcelLoaderServletRegistrar(String dataTaskUriPrefix, ExcelLoaderManager excelLoaderManager) {
        this.dataTaskUriPrefix = dataTaskUriPrefix;
        this.excelLoaderManager = excelLoaderManager;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        ExcelLoaderServlet servlet = new ExcelLoaderServlet(excelLoaderManager);
        ServletRegistration.Dynamic registration = servletContext.addServlet("ExcelLoaderServlet", servlet);
        registration.addMapping(dataTaskUriPrefix + ExcelLoaderServlet.LIST_DATA_TASK);
        registration.addMapping(dataTaskUriPrefix + ExcelLoaderServlet.GET_DATA_TASK);
        registration.addMapping(dataTaskUriPrefix + ExcelLoaderServlet.CLEAR_DATA_TASK);
        registration.setLoadOnStartup(1);
        log.info("yfcloud controller open status api success: ExcelLoader");
    }
}
